// com.lesso.cyg.sunflower.infra.repository.impl.EmployeeDeviceRelRepositoryImpl.java
package com.lesso.cyg.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.cyg.sunflower.domain.repository.EmployeeDeviceRelRepository;
import com.lesso.cyg.sunflower.infra.mapper.EmployeeDeviceRelMapper;
import com.lesso.cyg.sunflower.model.EmployeeDeviceRel;
import com.lesso.cyg.sunflower.pojo.dto.EmployeeDeviceRelQueryDTO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 员工考勤设备关系Repository实现类
 */
@Repository
public class EmployeeDeviceRelRepositoryImpl implements EmployeeDeviceRelRepository {

    @Resource
    private EmployeeDeviceRelMapper employeeDeviceRelMapper;

    @Override
    public Integer save(EmployeeDeviceRel entity) {
        return employeeDeviceRelMapper.insert(entity);
    }

    @Override
    public void removeById(Long id) {
        employeeDeviceRelMapper.deleteById(id);
    }

    @Override
    public Integer update(EmployeeDeviceRel entity) {
        return employeeDeviceRelMapper.updateById(entity);
    }

    /**
     * 分页查询员工考勤设备关系
     */
    @Override
    public Page<EmployeeDeviceRel> selectPage(EmployeeDeviceRelQueryDTO queryDTO) {
        int pageNum = queryDTO.getPage().getCurrent();
        int pageSize = queryDTO.getPage().getSize();
        int offset = (pageNum - 1) * pageSize;

        // 查询当前页数据
        List<EmployeeDeviceRel> records = employeeDeviceRelMapper.selectByPage(
                offset,
                pageSize,
                queryDTO.getEmployeeId(),
                queryDTO.getDeviceCode(),
                queryDTO.getAuthorizationStatus()
        );

        // 查询总条数
        Integer total = employeeDeviceRelMapper.selectCount(
                queryDTO.getEmployeeId(),
                queryDTO.getDeviceCode(),
                queryDTO.getAuthorizationStatus()
        );

        // 封装分页结果
        Page<EmployeeDeviceRel> page = new Page<>(pageNum, pageSize);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }
}